package com.shwread.android.qysw.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shwread.android.qysw10000209.R;
import com.shwread.android.ui.widget.MyGoodsAllView;
import com.shwread.android.ui.widget.MyGoodsNoDeliverView;
import com.shwread.android.ui.widget.MyGoodsNoPayView;
import com.shwread.android.ui.widget.MyHotelAllView;
import com.shwread.android.ui.widget.MyTicketNoCommentView;
import com.shwread.android.ui.widget.MyTicketNoDeliverView;
import com.shwread.android.ui.widget.MyTicketNoPayView;

import java.util.ArrayList;
import java.util.List;

import logic.util.IRefreshViewListener;


/**
 * 我的商品主页面
 *
 * @author zhangnannan
 */

public class MyGoodsActivity
		extends
		BaseBusinessActivity
		implements
		OnClickListener
{
	private BaseBusinessActivity bActivity;
	private Context              context;
	private TextView             tvTitle;
	private Button               butBack;

	private TextView             tvTabAll, tvTabNoPay, TvTabNoDeliver, tvTabNoComment;

	private ViewPager            viewPager;
	private List<View>           lvViewPager;
	private ViewPagerAdapter     viewPagerAdapter;

	private final int TAB_0 = 0;
	private final int TAB_1 = 1;
	private final int TAB_2 = 2;
	private final int TAB_3 = 3;
	private int ticketcurrentPage = 0;
	private int goodscurrentPage = 0;
	private int hotelcurrentPage = 0;
	//订单
	private MyGoodsAllView myGoodsAllView = null;
	private MyGoodsNoPayView myGoodsNoPayView = null;
	private MyGoodsNoDeliverView myGoodsNoDeliverView = null;
	//门票订单
	private MyTicketNoPayView myTicketNoPayView = null;
	private MyTicketNoDeliverView myTicketNoDeliverView = null;
	private MyTicketNoCommentView myTicketNoCommentView = null;
	//酒店订单
	private MyHotelAllView myHotelAllView = null;
	private MyHotelAllView myHotelAllView1 = null;
	private MyHotelAllView myHotelAllView2 = null;
	private MyHotelAllView myHotelAllView3 = null;
//	private MyHotelNoPayView myHotelNoPayView = null;
//	private MyHotelFinishedView myHotelFinishedView = null;
//	private MyHotelPayedView myHotelPayedView = null;


	//dialog用到变量
	private LinearLayout popLlScienOrderSelect,popLlGoodOrderSelect,popLlCancel,popLlHotelOrder;
	private Button popBtScienOrder,popBtGoodOrder,popBtCancel,popBtHotelOrder;
	private List<Button> btnList = null;
	private Dialog dialog = null;

	private TextView tvOtherOrder;

	//订单类型标记，1是默认值，代表门票订单，2是酒店订单，3是商品订单
	private int orderType = 1;

	@Override
	public Handler getUIHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_goods);

		context = this;
		bActivity = this;
		findView();
		addListener();
		initData();
	}


	private void findView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		butBack = (Button) findViewById(R.id.common_header_back);
		tvTitle.setText("我的订单");
		butBack.setVisibility(View.VISIBLE);

		tvTabAll = (TextView) findViewById(R.id.my_goods_tab_all);
		tvTabAll.setVisibility(View.GONE);
		tvTabNoPay = (TextView) findViewById(R.id.my_goods_tab_no_pay);
		TvTabNoDeliver = (TextView) findViewById(R.id.my_goods_tab_no_deliver);
		tvTabNoComment = (TextView) findViewById(R.id.my_goods_tab_no_comment);
		tvTabNoPay.setSelected(true);

		viewPager = (ViewPager) findViewById(R.id.my_goods_view);
		viewPager.setOffscreenPageLimit(4);


		//其他订单，切换酒店订单，门票订单，商品订单
		tvOtherOrder = (TextView) findViewById(R.id.tvOtherOrder);
		tvOtherOrder.setVisibility(View.VISIBLE);

		dialog = createDialog(MyGoodsActivity.this);
		popLlScienOrderSelect.setOnClickListener(this);
		popLlGoodOrderSelect.setOnClickListener(this);
		popLlCancel.setOnClickListener(this);
		popLlHotelOrder.setOnClickListener(this);

		popBtScienOrder.setOnClickListener(this);
		popBtGoodOrder.setOnClickListener(this);
		popBtCancel.setOnClickListener(this);
		popBtHotelOrder.setOnClickListener(this);
	}

	private void initData() {

		myTicketNoPayView = new MyTicketNoPayView(this);
		myTicketNoDeliverView = new MyTicketNoDeliverView(this);
		myTicketNoCommentView = new MyTicketNoCommentView(this);

		myGoodsAllView = new MyGoodsAllView(this);
		myGoodsNoPayView = new MyGoodsNoPayView(this);
		myGoodsNoDeliverView = new MyGoodsNoDeliverView(this);


		myHotelAllView = new MyHotelAllView(this,"-1");
		myHotelAllView1 = new MyHotelAllView(this,"1");
		myHotelAllView2 = new MyHotelAllView(this,"2");
		myHotelAllView3 = new MyHotelAllView(this,"6");
//		myHotelNoPayView = new MyHotelNoPayView(this);
//		myHotelPayedView = new MyHotelPayedView(this);
//		myHotelFinishedView = new MyHotelFinishedView(this);

		lvViewPager = new ArrayList<View>();
		lvViewPager.add(myTicketNoPayView);
		lvViewPager.add(myTicketNoDeliverView);
		lvViewPager.add(myTicketNoCommentView);
		viewPagerAdapter = new ViewPagerAdapter();
		viewPager.setAdapter(viewPagerAdapter);
		viewPager.setCurrentItem(TAB_0);

	}

	private void addListener() {
		butBack.setOnClickListener(this);
		tvOtherOrder.setOnClickListener(this);
		tvTabAll.setOnClickListener(this);
		tvTabNoPay.setOnClickListener(this);
		TvTabNoDeliver.setOnClickListener(this);
		tvTabNoComment.setOnClickListener(this);
		viewPager.setOnPageChangeListener(new OnPageChangeListener()
		{

			@Override
			public void onPageSelected(int arg0) {
				setSelect(arg0);
				((IRefreshViewListener)(lvViewPager.get(arg0))).refreshView();
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				finish();
				break;
			case R.id.my_goods_tab_all:
				setSelect(TAB_0);
				viewPager.setCurrentItem(TAB_0);
				break;
			case R.id.my_goods_tab_no_pay:
				if (orderType==2){
					setSelect(TAB_1);
					viewPager.setCurrentItem(TAB_1);
				}else{
					setSelect(TAB_0);
					viewPager.setCurrentItem(TAB_0);
				}
				break;
			case R.id.my_goods_tab_no_deliver:
				if (orderType==2){
					setSelect(TAB_2);
					viewPager.setCurrentItem(TAB_2);
				}else{
					setSelect(TAB_1);
					viewPager.setCurrentItem(TAB_1);
				}
				break;
			case R.id.my_goods_tab_no_comment:
				if (orderType==2){
					setSelect(TAB_3);
					viewPager.setCurrentItem(TAB_3);
				}else{
					setSelect(TAB_2);
					viewPager.setCurrentItem(TAB_2);
				}
				break;
			case R.id.tvOtherOrder:
				dialog.show();
				break;
			case R.id.popLlScienOrderSelect:
			case R.id.popBtScienOrder:
				tvTabAll.setVisibility(View.GONE);
				if (orderType!=1) {
					orderType=1;
					choseBuyMode("景点订单门票", popBtScienOrder);
					tvTabNoPay.setText("待使用");
					TvTabNoDeliver.setText("已使用");
					tvTabNoComment.setText("已过期");
					lvViewPager.clear();
					viewPager.removeAllViews();
					lvViewPager.add(myTicketNoPayView);
					lvViewPager.add(myTicketNoDeliverView);
					lvViewPager.add(myTicketNoCommentView);
					viewPagerAdapter.notifyDataSetChanged();
					setSelect(ticketcurrentPage);
					viewPager.setCurrentItem(ticketcurrentPage);
				}else{
					dialog.dismiss();
				}
				break;
			case R.id.popLlHotelOrder:
			case R.id.popBtHotelOrder:
				if (orderType!=2) {
					orderType=2;
					choseBuyMode("酒店订单", popBtHotelOrder);
					tvTabAll.setVisibility(View.VISIBLE);
					tvTabAll.setText("全部");
					tvTabNoPay.setText("未付款");
					TvTabNoDeliver.setText("已付款");
					tvTabNoComment.setText("已完成");
					lvViewPager.clear();
					viewPager.removeAllViews();
					lvViewPager.add(myHotelAllView);
					lvViewPager.add(myHotelAllView1);
					lvViewPager.add(myHotelAllView2);
					lvViewPager.add(myHotelAllView3);
//					lvViewPager.add(myHotelNoPayView);
//					lvViewPager.add(myHotelPayedView);
//					lvViewPager.add(myHotelFinishedView);
					viewPagerAdapter.notifyDataSetChanged();
					setSelect(hotelcurrentPage);
					viewPager.setCurrentItem(hotelcurrentPage);
				}else{
					dialog.dismiss();
				}
				break;
			case R.id.popLlGoodOrderSelect:
			case R.id.popBtGoodOrder:
				tvTabAll.setVisibility(View.GONE);
				if (orderType!=3) {
					orderType=3;
					choseBuyMode("我的商品订单", popBtGoodOrder);
					tvTabNoPay.setText("全部");
					TvTabNoDeliver.setText("待付款");
					tvTabNoComment.setText("待发货");
					lvViewPager.clear();
					viewPager.removeAllViews();
					lvViewPager.add(myGoodsAllView);
					lvViewPager.add(myGoodsNoPayView);
					lvViewPager.add(myGoodsNoDeliverView);
					viewPagerAdapter.notifyDataSetChanged();
					setSelect(goodscurrentPage);
					viewPager.setCurrentItem(goodscurrentPage);
				}else{
					dialog.dismiss();
				}
				break;
			case R.id.popLlCancel:
			case R.id.popBtCancel:
				dialog.dismiss();
				break;
			default:
				break;
		}

	}

	private void choseBuyMode(String mode, Button btn) {
		setBuyModeChecked(btn);
		dialog.dismiss();
	}
	//设置订单种类形式dialog的选中图标
	private void setBuyModeChecked(Button btn) {
		Drawable checked = getResources().getDrawable(R.drawable.selected_buymode);
		checked.setBounds(0, 0, checked.getMinimumWidth(), checked.getMinimumHeight());
		for (int i = 0; i < btnList.size(); i++) {
			if (btnList.get(i) == btn) {
				btnList.get(i).setCompoundDrawables(null, null, checked, null);
			} else {
				btnList.get(i).setCompoundDrawables(null, null, null, null);
			}
		}
	}

	private void setSelect(int a) {
		if (orderType==2){
			hotelcurrentPage=a;
			tvTabAll.setSelected(a == TAB_0 ? true : false);
			tvTabNoPay.setSelected(a == TAB_1 ? true : false);
			TvTabNoDeliver.setSelected(a == TAB_2 ? true : false);
			tvTabNoComment.setSelected(a == TAB_3 ? true : false);
		}else {
			if (orderType==1){
				ticketcurrentPage=a;
			}
			if (orderType==3){
				goodscurrentPage=a;
			}
			tvTabNoPay.setSelected(a == TAB_0 ? true : false);
			TvTabNoDeliver.setSelected(a == TAB_1 ? true : false);
			tvTabNoComment.setSelected(a == TAB_2 ? true : false);
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		((IRefreshViewListener)(lvViewPager.get(viewPager.getCurrentItem()))).refreshView();
	}
	class ViewPagerAdapter
			extends
			PagerAdapter
	{
		private int mChildCount = 0;

		@Override
		public void notifyDataSetChanged() {
			mChildCount = getCount();
			super.notifyDataSetChanged();
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lvViewPager.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			if ( mChildCount > 0) {
				mChildCount --;
				return POSITION_NONE;
			}
			return super.getItemPosition(object);
		}

		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(lvViewPager.get(position));
			return lvViewPager.get(position);
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(lvViewPager.get(position));
		}
	}

	private Dialog createDialog(Context context) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout dialogView = (LinearLayout) inflater.inflate(
				R.layout.order_confirm_pop, null);
		popLlScienOrderSelect = (LinearLayout) dialogView.findViewById(R.id.popLlScienOrderSelect);
		popLlGoodOrderSelect = (LinearLayout) dialogView.findViewById(R.id.popLlGoodOrderSelect);
		popLlHotelOrder = (LinearLayout) dialogView.findViewById(R.id.popLlHotelOrder);
		popLlCancel = (LinearLayout) dialogView.findViewById(R.id.popLlCancel);

		popBtScienOrder = (Button) dialogView.findViewById(R.id.popBtScienOrder);
		popBtGoodOrder = (Button) dialogView.findViewById(R.id.popBtGoodOrder);
		popBtCancel = (Button) dialogView.findViewById(R.id.popBtCancel);
		popBtHotelOrder = (Button) dialogView.findViewById(R.id.popBtHotelOrder);

		btnList = new ArrayList<>();
		btnList.add(popBtScienOrder);
		btnList.add(popBtHotelOrder);
		btnList.add(popBtGoodOrder);

		Dialog customDialog = new Dialog(context, R.style.custom_dialog);
		WindowManager.LayoutParams lp = customDialog.getWindow()
				.getAttributes();
		customDialog.getWindow().getDecorView().setPadding(0, 0, 0, 0);
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.gravity = Gravity.BOTTOM;
		customDialog.getWindow().setAttributes(lp);
		customDialog.setCancelable(true);
		customDialog.setCanceledOnTouchOutside(true);
		customDialog.setContentView(dialogView);
		return customDialog;
	}

	//验证码的状态待使用，已使用，已过期可能改变，所以要重写

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode==891){
			initData();
		}
	}
}
